//
//  JXMeViewController.m
//

#import "JXMeViewController.h"

#import "JXMCSUserManager.h"
#import "JXTool.h"

@interface JXMeViewController()<UIAlertViewDelegate>
@end

@implementation JXMeViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIImageView *backgroundView = [[UIImageView alloc] initWithFrame:self.view.frame];
    backgroundView.image = JXGetSuitableImage(@"me");
    backgroundView.userInteractionEnabled = YES;

    UIButton *exitBtn = [[UIButton alloc] init];
    [self setupLogoutBtnFrame:exitBtn];
    [exitBtn addTarget:self action:@selector(showLogoutAlert) forControlEvents:UIControlEventTouchUpInside];
    [backgroundView addSubview:exitBtn];

    UIButton *topChatBtn = [[UIButton alloc] init];
    topChatBtn.frame = CGRectMake((selfViewWidth - 44), 0, 44, 44);
    [backgroundView addSubview:topChatBtn];
    [topChatBtn addTarget:self action:@selector(requestCSForUI) forControlEvents:UIControlEventTouchUpInside];

    UIButton *chatBtn = [[UIButton alloc] init];
    [self setupChatBtnFrame:chatBtn];
    [backgroundView addSubview:chatBtn];
    [chatBtn addTarget:self action:@selector(requestCSForUI) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backgroundView];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self.view.subviews firstObject].height = selfViewHeight - sTabBarHeight;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
    [self.navigationController.navigationBar setBackgroundImage:JXImage(@"basicNav")
                                                  forBarMetrics:UIBarMetricsDefault];
    [sJXHUD hideHUD];
}

- (void)requestCSForUI {
    [[JXMCSUserManager sharedInstance] requestCSForUI:self.navigationController indexPath:1];
}

#pragma mark pravite methods

- (void)setupChatBtnFrame:(UIButton *)chatBtn {
    iPhoneType type = [UIDevice getCurrentIPhoneType];
    switch (type) {
        case iPhone5:
            chatBtn.frame = CGRectMake(selfViewWidth - 104, 310, 90, 50);
            break;
        case iPhone6:
            chatBtn.frame = CGRectMake(selfViewWidth - 125, 325, 120, 50);
            break;
        case iPhone6Plus:
            chatBtn.frame = CGRectMake(selfViewWidth - 140, 400, 120, 60);
            break;
        case iPhone4:
        default:
            chatBtn.frame = CGRectMake(selfViewWidth - 104, 305, 90, 50);
            break;
    }
}

- (void)setupLogoutBtnFrame:(UIButton *)exitBtn {
    iPhoneType type = [UIDevice getCurrentIPhoneType];
    switch (type) {
        case iPhone5:
            exitBtn.frame = CGRectMake(0, 425, selfViewWidth, 60);
            break;
        case iPhone6:
            exitBtn.frame = CGRectMake(0, 455, selfViewWidth, 60);
            break;
        case iPhone6Plus:
            exitBtn.frame = CGRectMake(0, 555, selfViewWidth, 60);
            break;
        default:
            exitBtn.frame = CGRectMake(0, 360, selfViewWidth, 60);
            break;
    }
}

- (void)showLogoutAlert {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"确认退出登录"
                                                        message:nil
                                                       delegate:self
                                              cancelButtonTitle:@"取消"
                                              otherButtonTitles:@"确认", nil];
    [alertView show];

}

#pragma mark- UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        [[JXMCSUserManager sharedInstance] logoutWithResponseBlock:^(BOOL success, id response) {
            AppDelegate *currentDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
            [currentDelegate showLoginWindow];
        }];
    }
}

@end
